﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using PostSharp.CodeModel;

namespace PostSharp4ViewState.Weaver
{
   internal sealed class PerTypeData
   {
      private readonly List<MemberData> _members = new List<MemberData>();

      public void AddMember(NamedDeclaration memberDef, PersistMode mode)
      {         
         _members.Add(new MemberData(memberDef, mode));
      }

      public bool RegisterLoadControlStateAdded { get; set; }      

      public bool IsEmpty(PersistMode mode)
      {
         return _members.Where(data => data.PersistMode == mode).Count() == 0;
      }

      public IEnumerable<T> GetMembers<T>(PersistMode mode)
         where T : NamedDeclaration
      {
         return
            _members.Where(data => data.PersistMode == mode && data.Member.GetType() == typeof (T)).Select(
               data => data.Member).OrderBy(member => member.Name).Cast<T>();
      }      

      internal sealed class MemberData
      {         
         public PersistMode PersistMode { get; private set; }
         public NamedDeclaration Member { get; private set; }

         public MemberData(NamedDeclaration member, PersistMode mode)
         {
            Member = member;
            PersistMode = mode;
         }
      }
   }      
}
